package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.CrThingRecord;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class CrThingRecordSDAO  extends BaseDAO {
	public CrThingRecordSDAO(){
		this(DB_CHECKRECORD);
	}
	public CrThingRecordSDAO(String dbFile){
		super(dbFile);
	}
	public CrThingRecordSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"CR_THING_RECORD("+
						 " ID  VARCHAR2(32) PRIMARY KEY,"+
						 " CHECKEVENT_ID VARCHAR2(32),"+
						 " THING_CLASS  VARCHAR2(16),"+
						 " NAME VARCHAR2(32),"+
						 " CODE VARCHAR2(32),"+
						 " THING_DESC VARCHAR2(64),"+
						 " ZP BLOB,"+
						 " OWNER VARCHAR2(32),"+
						 " OWNER_SFHM VARCHAR2(32),"+
						 " HOLDER VARCHAR2(32),"+
						 " HOLDER_SFHM VARCHAR2(32),"+
						 " MANUFACTURER VARCHAR2(32),"+
						 " PCSJ DATE,"+
						 " SJLY VARCHAR2(50),"+
						 " CREATE_TIME DATE"+
						 ")";
		
	}
	public void createTabbleIndex(){
		try {
			jdbcUtils.execute("CREATE INDEX IF NOT EXISTS INDEX_PCSJ_THRECORD on CR_THING_RECORD(PCSJ)");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(CrThingRecord entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getId())) {
				entity.setId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 修改
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int update(CrThingRecord entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "id");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(CrThingRecord entity) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM CR_THING_RECORD WHERE ID='"
					    +entity.getId()
		    		    +"'"
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}

	/**
	 * 分页查询
	 * @param sql
	 * @param pagingInfo
	 * @param entity
	 * @return 
	 * @throws SysException, AppException
	 */
	public  PagingResult<CrThingRecord> queryList(PagingInfo pagingInfo, CrThingRecord entity)
			throws SysException,AppException {
		PagingResult<CrThingRecord> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrThingRecord.class,
				pagingInfo, getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList分页查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<CrThingRecord> queryList(CrThingRecord entity) throws SysException,AppException {
	    List<CrThingRecord> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrThingRecord.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  CrThingRecord queryBean(CrThingRecord entity) throws SysException,AppException {
		CrThingRecord res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), CrThingRecord.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}

	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(CrThingRecord crThingRecord) {
		List<Object> qryList = new ArrayList<Object>();
		if (crThingRecord.getId() != null) {
			qryList.add(crThingRecord.getId());
		}
		if (crThingRecord.getCheckeventId() != null) {
			qryList.add(crThingRecord.getCheckeventId());
		}
		if (crThingRecord.getThingClass() != null) {
			qryList.add(crThingRecord.getThingClass());
		}
		if (crThingRecord.getName() != null) {
			qryList.add(crThingRecord.getName());
		}
		if (crThingRecord.getCode() != null) {
			qryList.add(crThingRecord.getCode());
		}
		if (crThingRecord.getThingDesc() != null) {
			qryList.add(crThingRecord.getThingDesc());
		}
		if (crThingRecord.getZp() != null) {
			qryList.add(crThingRecord.getZp());
		}
		if (crThingRecord.getOwner() != null) {
			qryList.add(crThingRecord.getOwner());
		}
		if (crThingRecord.getOwnerSfhm() != null) {
			qryList.add(crThingRecord.getOwnerSfhm());
		}
		if (crThingRecord.getHolder() != null) {
			qryList.add(crThingRecord.getHolder());
		}
		if (crThingRecord.getHolderSfhm() != null) {
			qryList.add(crThingRecord.getHolderSfhm());
		}
		if (crThingRecord.getManufacturer() != null) {
			qryList.add(crThingRecord.getManufacturer());
		}
		if (crThingRecord.getPcsj() != null) {
			qryList.add(crThingRecord.getPcsj());
		}
		if (crThingRecord.getSjly() != null) {
			qryList.add(crThingRecord.getSjly());
		}
		if (crThingRecord.getCreateTime() != null) {
			qryList.add(crThingRecord.getCreateTime());
		}
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(CrThingRecord crThingRecord) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.ID,a.CHECKEVENT_ID,a.THING_CLASS,a.NAME,a.CODE,a.THING_DESC,a.ZP,a.OWNER,a.OWNER_SFHM,a.HOLDER,a.HOLDER_SFHM,a.MANUFACTURER,a.PCSJ,a.SJLY,a.CREATE_TIME");
		sql.append(" from CR_THING_RECORD a where 1=1");
		if (crThingRecord.getId() != null) {
			sql.append(" and ID=?");
		}
		if (crThingRecord.getCheckeventId() != null) {
			sql.append(" and CHECKEVENT_ID=?");
		}
		if (crThingRecord.getThingClass() != null) {
			sql.append(" and THING_CLASS=?");
		}
		if (crThingRecord.getName() != null) {
			sql.append(" and NAME=?");
		}
		if (crThingRecord.getCode() != null) {
			sql.append(" and CODE=?");
		}
		if (crThingRecord.getThingDesc() != null) {
			sql.append(" and THING_DESC=?");
		}
		if (crThingRecord.getZp() != null) {
			sql.append(" and ZP=?");
		}
		if (crThingRecord.getOwner() != null) {
			sql.append(" and OWNER=?");
		}
		if (crThingRecord.getOwnerSfhm() != null) {
			sql.append(" and OWNER_SFHM=?");
		}
		if (crThingRecord.getHolder() != null) {
			sql.append(" and HOLDER=?");
		}
		if (crThingRecord.getHolderSfhm() != null) {
			sql.append(" and HOLDER_SFHM=?");
		}
		if (crThingRecord.getManufacturer() != null) {
			sql.append(" and MANUFACTURER=?");
		}
		if (crThingRecord.getPcsj() != null) {
			sql.append(" and PCSJ=?");
		}
		if (crThingRecord.getSjly() != null) {
			sql.append(" and SJLY=?");
		}
		if (crThingRecord.getCreateTime() != null) {
			sql.append(" and CREATE_TIME=?");
		}
		return sql.toString();
	}
}

